package TankAgent;

import jade.content.ContentManager;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.FIPAAgentManagement.FailureException;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.FIPAProtocolNames;
import jade.proto.SimpleAchieveREResponder;
import wwtp.ontology.Laminate;
import wwtp.ontology.Origin;
import wwtp.ontology.Volume;
import wwtp.ontology.VolumeOf;

public class LaminateBehaviour extends TickerBehaviour{

	protected MessageTemplate mt;
	protected ContentManager manager;

	public LaminateBehaviour(Agent arg0, long arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
		manager = myAgent.getContentManager();
	}

	@Override
	protected void onTick() {
		// TODO Auto-generated method stub
		if (((TankAgent)myAgent).actualVolumen > 0)
		{
			try{
				//Preguntar si se lamina
				ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
			    msg.setConversationId("LaminateProtocol");
			    msg.setLanguage(((TankAgent)myAgent).codec.getName());
			    msg.setOntology(((TankAgent)myAgent).ontology.getName());

			    msg.addReceiver(new AID( ((TankAgent)myAgent).dbDirectWater.getLaminateWWTP(myAgent.getLocalName()),AID.ISLOCALNAME));

			    VolumeOf vof = new VolumeOf();
			    Origin o = new Origin();
			    o.setName(myAgent.getLocalName());
			    Volume v = new Volume();
			    v.setLitres((float)((TankAgent)myAgent).actualVolumen);
			    vof.setOrigin(o);
			    vof.setVolume(v);

			    manager.fillContent(msg,vof);
				myAgent.send(msg);

				//Create a Behaviour to wait for a Request of action from the wwtp

				mt = MessageTemplate.and(MessageTemplate.MatchConversationId("LaminateProtocol-action"),SimpleAchieveREResponder.createMessageTemplate(FIPAProtocolNames.FIPA_REQUEST));
				myAgent.addBehaviour(new SimpleAchieveREResponder(myAgent, mt){

					@Override
					protected ACLMessage prepareResponse(ACLMessage request)
							throws NotUnderstoodException, RefuseException {

						try{
							//Do the action Requested by the wwtp

							Action action = (Action)manager.extractContent(request);

							if (action.getAction() instanceof Laminate){
								double litres = ((Laminate)action.getAction()).getVolume().getLitres();
								if (  litres > 0){
									System.out.println("Decision "+ request.getSender().getLocalName()+" : LAMINATE: "+ litres);
									((TankAgent)myAgent).laminate(litres);
								}

							}

						}
						catch (Exception e){
							e.printStackTrace();
						}

						ACLMessage response = request.createReply();
						response.setPerformative(ACLMessage.INFORM);
						return response;

					}

					@Override
					protected ACLMessage prepareResultNotification(ACLMessage request,
							ACLMessage response) throws FailureException {
						// TODO Auto-generated method stub

						response.setPerformative(ACLMessage.INFORM);
						return response;
					}

				});
			}
			catch (Exception e){
				e.printStackTrace();
			}


		}
	}

}
